package com.shi.controller;

import com.shi.entity.Type;
import com.shi.service.TypeServiceImpl;
import com.shi.utils.AppData;
import com.shi.utils.Utils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;

/**
 * Author 史豪通
 * Date 2024/08/30 10:08
 */
@RestController
@RequestMapping("/type")
public class TypeController {
    @Resource
    TypeServiceImpl service;

    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    @RequestMapping("/insertType")
    public int insertType(@RequestBody Type type) {
        int i = service.insertType(type);
        redisTemplate.opsForHash().put("type", type.getId(), type);
        return i;
    }

    @RequestMapping("/deleteType/{id}")
    public int deleteType(@PathVariable("id") int id) {
        if (selectOneType(id).getState() == 1) return -1;
        if (selectOneType(id).getParentId() == 0) {
            AppData appData = selectChildTypeByParentId(id, 1, 99999);
            List<Object> child = appData.getList();
            for (Object o : child) {
                Type type = (Type) o;
                if (type.getState() == 1) {
                    return -2;
                }
            }
            for (Object o : child) {
                Type type = (Type) o;
                redisTemplate.opsForHash().delete("type", type.getId());
            }
        }

        redisTemplate.opsForHash().delete("type", id);
        return service.deleteType(id);
    }

    @RequestMapping("/updateType")
    public int updateType(@RequestBody Type type) {
        redisTemplate.opsForHash().put("type", type.getId(), type);
        return service.updateType(type);
    }

    @RequestMapping("/selectParentType")
    public AppData selectParentType(int pageNum, int pageSize, @RequestParam(value = "parentId", defaultValue = "0", required = false) int parentId) {
        List<Object> list = redisTemplate.opsForHash().values("type");
        if (!list.isEmpty()) {
            ArrayList<Type> redisList = new ArrayList<>();
            for (Object o : list) {
                Type type = (Type) o;
                if (type.getParentId() == parentId) {
                    redisList.add(type);
                }
            }
            if (!redisList.isEmpty()) {
                System.out.println("使用了redis缓存...");
                Collections.sort(redisList, new Comparator<Type>() {
                    @Override
                    public int compare(Type o1, Type o2) {
                        return Integer.compare(o1.getId(), o2.getId());
                    }
                });
                return new AppData(Utils.getList(new ArrayList<>(redisList), pageNum, pageSize), redisList.size());
            }
        }

        System.out.println("请求数据库...");
        list = new ArrayList<>(service.selectParentType(parentId));

        for (Object o : list) {
            Type type = (Type) o;
            redisTemplate.opsForHash().put("type", type.getId(), type);
        }
        return new AppData(Utils.getList(new ArrayList<>(list), pageNum, pageSize), list.size());
    }

    @RequestMapping("/selectOneType/{id}")
    public Type selectOneType(@PathVariable("id") int id) {
        Type type = (Type) redisTemplate.opsForHash().get("type", id);
        if (type != null) return type;
        return service.selectOneType(id);
    }

    @RequestMapping("/selectChildTypeByParentId/{parentId}")
    public AppData selectChildTypeByParentId(@PathVariable("parentId") int parentId, int pageNum, int pageSize) {
        List<Object> list = redisTemplate.opsForHash().values("type");
        if (!list.isEmpty()) {
            ArrayList<Type> redisList = new ArrayList<>();
            for (Object o : list) {
                Type type = (Type) o;
                if (type.getParentId() == parentId) {
                    redisList.add(type);
                }
            }
            if (!redisList.isEmpty()) {
                System.out.println("使用了redis缓存...");
                Collections.sort(redisList, new Comparator<Type>() {
                    @Override
                    public int compare(Type o1, Type o2) {
                        return Integer.compare(o1.getId(), o2.getId());
                    }
                });
                return new AppData(Utils.getList(new ArrayList<>(redisList), pageNum, pageSize), redisList.size());
            }
        }

        System.out.println("请求数据库...");
        list = new ArrayList<>(service.selectChildTypeByParentId(parentId));

        for (Object o : list) {
            Type type = (Type) o;
            redisTemplate.opsForHash().put("type", type.getId(), type);
        }
        return new AppData(Utils.getList(new ArrayList<>(list), pageNum, pageSize), list.size());
    }

    @RequestMapping("/changeTypeState")
    public int changeTypeState(@RequestBody Type type) {
        if (type.getParentId() != 0 && selectOneType(type.getParentId()).getState() == -1) return -1;
        if (type.getParentId() == 0 && type.getState() == 1) {
            List<Object> list = redisTemplate.opsForHash().values("type");
            if (!list.isEmpty()) {
                for (Object o : list) {
                    Type t = (Type) o;
                    if (t.getParentId() == type.getId()) {
                        t.setState(-type.getState());
                        redisTemplate.opsForHash().put("type", t.getId(), t);
                    }
                }
            }
        }
        type.setState(-type.getState());
        redisTemplate.opsForHash().put("type", type.getId(), type);
        type.setState(-type.getState());
        return service.changeTypeState(type);
    }

    @RequestMapping("/conditionSearch")
    public AppData conditionSearch(int pageNum, int pageSize, @RequestParam(value = "searchType", defaultValue = "", required = false) String searchType, int currentParentId) {
        List<Object> list = redisTemplate.opsForHash().values("type");
        if (!list.isEmpty()) {
            ArrayList<Type> redisList = new ArrayList<>();
            for (Object o : list) {
                Type type = (Type) o;
                if (type.getName().contains(searchType) && type.getParentId() == currentParentId) {
                    redisList.add(type);
                }
            }
            if (!redisList.isEmpty()) {
                System.out.println("使用了redis缓存...");
                Collections.sort(redisList, new Comparator<Type>() {
                    @Override
                    public int compare(Type o1, Type o2) {
                        return Integer.compare(o1.getId(), o2.getId());
                    }
                });
                return new AppData(Utils.getList(new ArrayList<>(redisList), pageNum, pageSize), redisList.size());
            }
        }

        System.out.println("请求数据库...");
        Map<String, String> map = new HashMap<>();
        map.put("searchType", searchType);
        map.put("currentParentId", String.valueOf(currentParentId));
        list = new ArrayList<>(service.conditionSearch(map));

        for (Object o : list) {
            Type type = (Type) o;
            redisTemplate.opsForHash().put("type", type.getId(), type);
        }
        return new AppData(Utils.getList(new ArrayList<>(list), pageNum, pageSize), list.size());
    }
}
